package com.wuliqing.android.zte.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.wuliqing.android.zte.myapplication.R;

/**
 * Created by 10172915 on 2015/7/10.
 */
public class MaskFilterView extends View {
    private static final int RECT_SIZE = 400;
    private Paint mPaint;// 画笔
    private Context mContext;// 上下文环境引用
    private int screenW, screenH;
    private Bitmap mBitmap;
    private Bitmap shadowBitmap;// 阴影位图
    private int left, top, right, bottom;//

    public MaskFilterView(Context context) {
        this(context, null);
    }

    public MaskFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        screenW = wm.getDefaultDisplay().getWidth();//dm.widthPixels;
        screenH = wm.getDefaultDisplay().getHeight();//dm.heightPixels;

        // 初始化画笔
        initPaint();

        // 初始化资源
        initRes(context);
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        // 实例化画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.DKGRAY);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        // 设置画笔遮罩滤镜
        mPaint.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL));
    }

    /**
     * 初始化资源
     */
    private void initRes(Context context) {
        /*
         * 计算位图绘制时左上角的坐标使其位于屏幕中心
		 */
        left = screenW / 2 - RECT_SIZE / 2;
        top = screenH / 2 - RECT_SIZE / 2;
        right = screenW / 2 + RECT_SIZE / 2;
        bottom = screenH / 2 + RECT_SIZE / 2;
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.cheese_3);
        mBitmap = Bitmap.createBitmap(bitmap, left, top, 400, 400);
        // 获取位图的Alpha通道图
        shadowBitmap = mBitmap.extractAlpha();

//        left = screenW / 2 - mBitmap.getWidth() / 2;
//        top = screenH / 2 - mBitmap.getHeight() / 2;
//        right = screenW / 2 + mBitmap.getWidth() / 2;
//        bottom = screenH / 2 + mBitmap.getHeight() / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
// 先绘制阴影
        canvas.drawBitmap(shadowBitmap, left, top, mPaint);

        // 画一个矩形
//        canvas.drawRect(left, top, right, bottom, mPaint);
        canvas.drawBitmap(mBitmap, left, top, mPaint);
    }
}
